<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearList"
    >
      <template #btns>
        <el-button
          v-if="meta.indexOf('OrderListExport') > -1"
          :loading="subLoading"
          type="primary"
          @click="handleExport"
          >{{ translate("export") }}</el-button
        >
      </template>
    </QueryForm>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table mt20"
    >
      <el-table-column
        align="center"
        :label="translate('orderNum')"
        min-width="220"
        prop="id"
        show-overflow-tooltip
      >
        <template v-slot="scope">
          <span class="primaryColor pointer" @click="handleDetail(scope.row)">{{
            scope.row.id
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('memberInfo')"
        min-width="220"
        prop="memberInfo"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('productName')"
        min-width="120"
        prop="productName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('priceUnit')"
        min-width="100"
        prop="price"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('realPayUni')"
        min-width="100"
        prop="realPrice"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('payMethod')"
        min-width="120"
        prop="payMethodName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('payTime')"
        min-width="160"
        prop="payTime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('refundPince')"
        min-width="100"
        prop="refundMoney"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('refundTime')"
        min-width="160"
        prop="refundTime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('orderTime')"
        min-width="160"
        prop="createTime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('bz')"
        min-width="120"
        prop="note"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('orderStatus')"
        min-width="120"
        prop="payStatusName"
        fixed="right"
        show-overflow-tooltip
      />
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <Detail ref="DetailRef" />
  </div>
</template>

<script>
import { queryFormConfig } from './config'
import { orderStatusConfig, orderTypeConfig, configList, momMOrderList, momMOrderListEx } from '@/api'
import dayjs from 'dayjs'
import Detail from './orderDetail.vue'
export default {
  components: {
    Detail
  },
  data() {
    return {
      list: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      meta: [],
      listLoading: false,
      queryFormConfig,
      subLoading: false,

      filters: {
        selDate: []
      }
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    let time = []
    const start = new Date()
    const end = new Date()
    start.setTime(end.getTime() - 3600 * 1000 * 24 * 29)
    time = [dayjs(start).format('YYYY-MM-DD'), dayjs(end).format('YYYY-MM-DD')]
    this.$set(this.filters, 'selDate', time)
    this.getList()
    this.getPayMethods()
    this.getOrderStatus()
  },
  methods: {
    getOrderStatus() {
      orderStatusConfig({ param: {} }).then(res => {
        this.queryFormConfig.formItems[3].options = res.recordList || []
      })
    },
    getPayMethods() {
      configList({ param: { code: '', type: '2' } }).then(res => {
        this.queryFormConfig.formItems[4].options = res.recordList || []
      })
    },
    handleExport() {
      this.subLoading = true
      const { filters } = this
      const param = { ...filters }
      if (param.selDate.length > 0) {
        param.startTime = param.selDate[0] + ' 00:00:00'
        param.endTime = param.selDate[1] + ' 23:59:59'
      }
      momMOrderListEx({ param })
        .then((res) => {
          this.subLoading = false
          if (res.errorCode === '000000') {
            window.open(res.record.showUrl, '_blank')
          }
        })
        .catch(() => {
          this.subLoading = false
        })
    },
    handleDetail(item) {
      this.$refs.DetailRef.getOrderDetail(item.id)
      this.$refs.DetailRef.isShow = true
    },
    getList(page) {
      this.listLoading = true
      const { pagination, filters } = this
      const param = { ...filters }
      if (page) {
        this.pagination.page = page
      }
      if (param.selDate.length > 0) {
        param.startTime = param.selDate[0] + ' 00:00:00'
        param.endTime = param.selDate[1] + ' 23:59:59'
      }
      momMOrderList({ pagination, param })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.total = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    handleSizeChange(val) {
      this.pagination.rows = val
      this.getList(1)
    },
    clearList() {
      this.filters = {
        selDate: []
      }
      this.getList(1)
    }
  }
}
</script>

<style>
</style>
